package swe.user;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import swe.article.DAO;

public class UserDAO implements DAO<User>{
	String filename;

	public UserDAO(String filename) {
		this.filename = filename;
	}

	
	public void save(List<User> user) {
		try {

			ObjectOutputStream os = new ObjectOutputStream(
					new FileOutputStream(filename));
			for (User u: user) {
				os.writeObject(u);
			}
			os.flush();
			os.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public List<User> read() {
		List<User> userList = new ArrayList<User>();
		User user = null;

		try {
			ObjectInputStream is = new ObjectInputStream(new FileInputStream(
					filename));

			while ((user = (User) is.readObject()) != null) {
				userList.add(user);
			}

			is.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return userList;
	}
}
